package com.thread;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class StaticExecutorTest {

    private static Map<String, String> map1 = new HashMap<>();
    private static Map<String, String> map2 = new HashMap<>();
    private static Map<String, String> map3 = new HashMap<>();
    private static Map<String, String> map4 = new HashMap<>();
    private static Map<String, String> map5 = new HashMap<>();

    static {
        System.out.println("start run....");
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        executorService.submit(() -> {
            for (int i = 0; i < 20; i++) {
                System.out.println("map1");
                map1.put("map1"+i, String.valueOf(i));
            }
        });
        executorService.submit(() -> {
            for (int i = 0; i < 20; i++) {
                System.out.println("map2");
                map1.put("map2"+i, String.valueOf(i));
            }
        });
        executorService.submit(() -> {
            for (int i = 0; i < 20; i++) {
                System.out.println("map3");
                map1.put("map3"+i, String.valueOf(i));
            }
        });
        executorService.submit(() -> {
            for (int i = 0; i < 20; i++) {
                System.out.println("map4");
                map1.put("map4"+i, String.valueOf(i));
            }
        });
        executorService.submit(() -> {
            for (int i = 0; i < 20; i++) {
                System.out.println("map5");
                map1.put("map5"+i, String.valueOf(i));
            }
        });
        executorService.shutdown();
        System.out.println("task start...");
        /*try {
            if(executorService.awaitTermination(5, TimeUnit.SECONDS)){
                executorService.shutdown();
            }
        } catch (InterruptedException e) {
            executorService.shutdownNow();
            throw new RuntimeException(e);
        }*/
        while (executorService.isShutdown()){
            try {
                System.out.println("waitint...");
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static void main(String[] args) {
        System.out.println("main start...");
        new StaticExecutorTest();
        System.out.println("main end...");
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("map1:" + map1);
        System.out.println("map2:" + map2);
        System.out.println("map3:" + map3);
        System.out.println("map4:" + map4);
        System.out.println("map5:" + map5);
    }

}
